package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfomationVo;
import cn.ffcs.uec.staff.dao.UecDocEduDao;
import cn.ffcs.uec.staff.model.UecDocEdu;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecDocEduDao")
@Transactional
public class UecDocEduDaoImpl extends BaseDaoImpl implements UecDocEduDao {

	@Override
	public PageInfo queryPageInfoByUecDocEdu(UecDocEdu uecDocEdu, int current,
			int pageSize) {
		if (null != uecDocEdu) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_DOC_EDU T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			//判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecDocEdu.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			
			if (!StrUtil.isNullOrEmpty(uecDocEdu.getPkPsndoc())) {//判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecDocEdu.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecDocEdu.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecDocEdu.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecDocEdu.class);
		}
		return null;
	}

	@Override
	public UecDocEdu findUecDocEdu(UecDocEdu uecDocEdu) {
		if(uecDocEdu != null) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_DOC_EDU T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			
			//判断这个对象的状态
			if (!StrUtil.isNullOrEmpty(uecDocEdu.getStatusCd())) {
				sb.append(" AND T.STATUS_CD = ? ");
				params.add(uecDocEdu.getStatusCd());
			}

			//判断这个对象是外包人员的数据还是合同制的数据
			if (!StrUtil.isNullOrEmpty(uecDocEdu.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(uecDocEdu.getNmStaffType());
			}
			
			if(!StrUtil.isNullOrEmpty(uecDocEdu.getEduId())) {
				sb.append(" AND T.EDU_ID = ? ");
				params.add(uecDocEdu.getEduId());
			}
			
			if(!StrUtil.isNullOrEmpty(uecDocEdu.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecDocEdu.getStaffId());
			}
			
			List<UecDocEdu> list = this.jdbcFindList(sb.toString(), params, UecDocEdu.class);
			
			if(list.size() > 0) {
				return list.get(0);
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	@Override
	public PageInfo queryPageInfoByUecDocEduOnApproval(UecDocEdu uecDocEdu,
			int current, int pageSize) {
		if (null != uecDocEdu) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_DOC_EDU T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_PROCESS);

			//判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecDocEdu.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			
			if (!StrUtil.isNullOrEmpty(uecDocEdu.getPkPsndoc())) {//判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecDocEdu.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecDocEdu.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecDocEdu.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecDocEdu.class);
		}
		return null;
	}

	@Override
	public UecDocEdu getUecDocEduByUecOutStaffInfo(
			UecOutStaffInfo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_DOC_EDU T WHERE 1 = 1 AND T.STATUS_CD = ? AND T.NM_STAFF_TYPE = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
			}
			List<UecDocEdu> list = this.jdbcFindList(sb.toString(), params,
					UecDocEdu.class);
			if (list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}
	
	
	@Override
	public List<UecDocEdu> getUecDocEduByUecOutStaffId(
			UecOutStaffInfo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_DOC_EDU T WHERE 1 = 1 AND T.STATUS_CD = ? AND T.NM_STAFF_TYPE = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
			}
			
		return this.jdbcFindList(sb.toString(), params,
						UecDocEdu.class);
		}
		return null;
	}

	@Override
	public List<UecDocEdu> getUecDocEduByUecOutAndStaffId(UecOutStaffInfomationVo uecOutStaffInfomationVo) {
		if (null != uecOutStaffInfomationVo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_DOC_EDU T WHERE 1 = 1 AND T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			/*if(!StrUtil.isNullOrEmpty(uecOutStaffInfomationVo.getStaffId())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecOutStaffInfomationVo.getStaffId());
				sb.append(" AND T.NM_STAFF_TYPE IS NULL ");
			}*/
			
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfomationVo.getOutStaffId()) && uecOutStaffInfomationVo.getOutStaffId()>0) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfomationVo.getOutStaffId());
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			
		return this.jdbcFindList(sb.toString(), params,
						UecDocEdu.class);
		}
		return null;
	}

	@Override
	public List<UecDocEdu> findUecDocEduByPkPsndoc(StaffAndOutStaff staffAndOutStaff) {
		if (null != staffAndOutStaff) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_DOC_EDU T WHERE 1 = 1 AND T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			
			if (!StrUtil.isNullOrEmpty(staffAndOutStaff.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(staffAndOutStaff.getPkPsndoc());
			}
			sb.append(" ORDER BY T.BEGINDATE");
		return this.jdbcFindList(sb.toString(), params,
						UecDocEdu.class);
		}
		return null;
	}
}
